using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizTalk.BuildGenerator.MetaData;

namespace BizTalk.BuildGenerator.BuildFrameworks.Default.CommandBuilders.BizTalkHost
{
    
    public class CreateHostBuilder : ICommandBuilder
    {
        private const string Command = "\t\t<BizTalk.BuildGenerator.Tasks.CreateHost HostName=\"{0}\" Trusted=\"{1}\" HostTracking=\"{2}\" HostType=\"{3}\" GroupName=\"{4}\" />";
        private const string Key = "<!-- @CreateHostCommand@ -->";


        #region ICommandBuilder Members
        /// <summary>
        /// Implements the Command Builder
        /// </summary>
        /// <param name="args"></param>
        /// <param name="fileBuilder"></param>
        public void Build(GenerationArgs args, StringBuilder fileBuilder)
        {
            var sb = new StringBuilder();

            foreach (var cmd in args.BizTalkHosts.Hosts.Select(host => new List<string>
                                                                           {
                                                                               host.Name, host.HostTrusted.ToString(), host.TrackingEnabled.ToString(), host.HostType.ToString(), host.WindowsGroupName
                                                                           }).Select(a => string.Format(Command, a.ToArray())))
            {
                sb.AppendLine(cmd);
            }

            fileBuilder.Replace(Key, sb.ToString());
        }

        #endregion
    }
}
